<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 按钮项 -->
      <el-row id="row">
        <el-button type="primary" class="btn" @click="addCateDiaog"
          >添加分类
        </el-button>
      </el-row>
      <!-- 表格 -->
      <zk-table
        :data="goodsList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-check" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-close" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operation">
          <el-row>
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="warning" icon="el-icon-delete">删除</el-button>
          </el-row>
        </template>
      </zk-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="questInfo.pagenum"
        :page-sizes="[5, 10, 20]"
        :page-size="questInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类目录 -->
    <el-dialog title="添加分类" :visible.sync="addCateDiaogVisible" width="50%" @close="addCatecolse">
      <el-form :model="addCateForm" label-width="80px" :rules="addCateRules" ref="addCateFormRef" >
        <el-form-item label="分类名称" prop="add_cate">
          <el-input  v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-cascader
            v-model="cascadelist"
            :options="ParentCateList"
            :props="cascaderProps"
            @change="handleChange" clearable change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatecolse">取 消</el-button>
        <el-button type="primary" @click="addCateok"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
/*eslint-disable*/

export default {
  data() {
    return {
      //验证规则
      addCateRules: {
        add_cate: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      // 二级对话框长度
      formLabelWidth: "100px",
      addCateForm: {
        cat_name: "",
        cat_pid: '',
        cat_level: '',
      },
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          // 定义为模板列
          type: "template",
          // 使用的模板名称
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "operation",
        },
      ],
      // 级联选择其
      cascadelist:[],
      cascaderProps:{
        expandTrigger: 'hover',
        value:'cat_id',
        children:'children',
        label:'cat_name'
      },
      questInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 商品分类列表
      goodsList: [],
      // 总数据数
      total: 0,
      // 控制分类目录对话框
      addCateDiaogVisible: false,
      // 父级分类数据列表
      ParentCateList: [],
    };
  },
  // 生命周期函数 页面激活时触发
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("/categories", {
        params: this.questInfo,
      });
      this.goodsList = res.data.result;
      this.total = res.data.total;
    },
    // 获得父类数据列表
    async getParentCateList() {
      const { data: res } = await this.$http.get("/categories", {params:{ type: 2 }});
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("发生未知错误");
      }
      this.ParentCateList = res.data;
    },
    // 添加分类对话框
    addCateDiaog() {
      this.getParentCateList();
      this.addCateForm.cat_pid = 0
         
         this.addCateForm.cat_level = 0
      this.addCateDiaogVisible = true;
    },
    // 监听 pagrsize 事件
    handleSizeChange(newsize) {
      this.questInfo.pagesize = newsize;
      this.getGoodsList();
    },
    handleCurrentChange(newPage) {
      this.questInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 级联选择器发生变化时触发
    handleChange(){
      console.log(this.cascadelist);
      // 如果 cascadelist 数组中的length 大于0 证明有选择父类分级
       if(this.cascadelist.length > 0){
         this.addCateForm.cat_pid = this.cascadelist[
           this.cascadelist.length -1 
         ]
         this.addCateForm.cat_level = this.cascadelist.length
         return
       }else{
         this.addCateForm.cat_pid = 0
         
         this.addCateForm.cat_level = 0
         return  
       }                                        
    },
    async addCateok(){
      console.log(this.addCateForm);
      //表单预验证
      // this.$refs.addCateFormRef.validate(async v =>{
      //   if(!v) return
        const {data:res} = await this.$http.post('/categories', this.addCateForm)
        if(res.meta.status !== 201){
          return this.$message.error('未知错误')
        
        }else{
          return this.$message.success('成功')}
      // })
    },
    addCatecolse(){
      this.addCateDiaogVisible = false
      this.cascadelist=[]
        this.addCateForm.cat_pid = 0
         this.addCateForm.cat_name=''
         this.addCateForm.cat_level = 0
    }
  },
};
</script>

<style lang="less" scoped>
.btn {
  float: left;
  margin-bottom: 20px;
}
</style>
